package com.zingrow.common.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class PropertiesCommonUtil {

	/**
	 * 读取Properties文件获取Key对应的值
	 * add by ChenPengzhen
	 * */
	public static String getValue(String fileName, String key) {
		String savePath = PropertiesCommonUtil.class.getResource(fileName).getPath();
		Properties properties = new Properties();
		try {
			//转换路径格式，防止空格引起的路径错误
			savePath = java.net.URLDecoder.decode(savePath, "UTF-8");
			InputStream inputStream = new FileInputStream(savePath);
			properties.load(inputStream);
			inputStream.close(); //关闭流
			return (String) properties.get(key);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}

	/***
	 * 读取资源文件,并处理中文乱码  
	 * @param filename 读取资源文件
	 */
	public static Properties readPropertiesFile(String filename) {
		String savePath = PropertiesCommonUtil.class.getResource(filename).getPath();
		Properties properties = new Properties();
		try {
			//转换路径格式，防止空格引起的路径错误
			savePath = java.net.URLDecoder.decode(savePath, "UTF-8");
			InputStream inputStream = new FileInputStream(savePath);
			properties.load(inputStream);
			inputStream.close(); //关闭流  
		} catch (IOException e) {
			e.printStackTrace();
		}
		// String username = properties.getProperty("allowUrls");  
		// String passsword = properties.getProperty("suffix");  
		String chinese = properties.getProperty("chinese");
		try {
			chinese = new String(chinese.getBytes("ISO-8859-1"), "UTF-8"); // 处理中文乱码  
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return properties;
	}

	//读取XML文件,并处理中文乱码  
	public static void readPropertiesFileFromXML(String filename) {
		String savePath = PropertiesCommonUtil.class.getResource(filename).getPath();
		Properties properties = new Properties();
		try {
			InputStream inputStream = new FileInputStream(savePath);
			properties.loadFromXML(inputStream);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String username = properties.getProperty("username");
		String passsword = properties.getProperty("password");
		String chinese = properties.getProperty("chinese"); //XML中的中文不用处理乱码，正常显示  
		System.out.println(username);
		System.out.println(passsword);
		System.out.println(chinese);
	}

	//写资源文件，含中文  
	public static void writePropertiesFile(String filename) {
		String savePath = PropertiesCommonUtil.class.getResource(filename).getPath();
		Properties properties = new Properties();
		try {
			OutputStream outputStream = new FileOutputStream(savePath);
			properties.setProperty("username", "myname");
			properties.setProperty("password", "mypassword");
			properties.setProperty("chinese", "中文");
			properties.store(outputStream, "author: shixing_11@sina.com");
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//写资源文件到XML文件，含中文    
	public static void writePropertiesFileToXML(String filename) {
		String savePath = PropertiesCommonUtil.class.getResource(filename).getPath();
		Properties properties = new Properties();
		try {
			OutputStream outputStream = new FileOutputStream(savePath);
			properties.setProperty("username", "myname");
			properties.setProperty("password", "mypassword");
			properties.setProperty("chinese", "中文");
			properties.storeToXML(outputStream, "author: 33@sina.com");
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		///spring/jdbc.properties
		//  String readfile = "d:" + File.separator + "readfile.properties";  
		//  String writefile = "d:" + File.separator + "writefile.properties";  
		//  String readxmlfile = "d:" + File.separator + "readxmlfile.xml";  
		//  String writexmlfile = "d:" + File.separator + "writexmlfile.xml";  
		// String readtxtfile = "d:" + File.separator + "readtxtfile.txt";  
		// String writetxtfile = "d:" + File.separator + "writetxtfile.txt";  

		readPropertiesFile("/properties/sessionfilter.txt"); //读取properties文件  
		// writePropertiesFile(writefile); //写properties文件  
		// readPropertiesFileFromXML(readxmlfile); //读取XML文件  
		//  writePropertiesFileToXML(writexmlfile); //写XML文件  
		// readPropertiesFile(readtxtfile); //读取txt文件  
		// writePropertiesFile(writetxtfile); //写txt文件  
	}
}
